package com.nidu.demo.file;

import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.nidu.demo.file.api.SysFileService;
import com.nidu.demo.file.convertor.FileConvertor;
import com.nidu.demo.file.dto.FileCO;
import com.nidu.demo.file.dto.FilePageQry;
import com.nidu.demo.file.executor.command.FileDeleteCmdExe;
import com.nidu.demo.file.executor.command.FileUploadCmdExe;
import com.nidu.demo.file.executor.query.FilePageQryExe;
import com.nidu.demo.file.executor.query.FileSingleQryExe;
import com.nidu.demo.file.model.File;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Set;

/**
 * 文件服务实现类
 *
 * @author auto
 */
@Service
@RequiredArgsConstructor
public class SysFileServiceImpl implements SysFileService {

    private final FileUploadCmdExe uploadCmdExe;
    private final FileDeleteCmdExe deleteCmdExe;
    private final FileSingleQryExe singleQryExe;
    private final FilePageQryExe pageQryExe;
    private final FileConvertor fileConvertor;

    @Override
    public SingleResponse<FileCO> uploadFile(MultipartFile file, String module) {
        File uploadedFile = uploadCmdExe.uploadFile(file, module);
        FileCO fileCO = fileConvertor.toClientObject(uploadedFile);
        return SingleResponse.of(fileCO);
    }

    @Override
    public SingleResponse<FileCO> uploadFile(MultipartFile file, String module, Integer storageType) {
        File uploadedFile = uploadCmdExe.uploadFile(file, module, storageType);
        FileCO fileCO = fileConvertor.toClientObject(uploadedFile);
        return SingleResponse.of(fileCO);
    }

    @Override
    public Response delete(Long id) {
        deleteCmdExe.execute(id);
        return Response.buildSuccess();
    }

    @Override
    public Response delete(Set<Long> ids) {
        deleteCmdExe.execute(ids);
        return Response.buildSuccess();
    }

    @Override
    public SingleResponse<FileCO> getById(Long id) {
        return singleQryExe.execute(id);
    }

    @Override
    public PageResponse<FileCO> page(FilePageQry qry) {
        return pageQryExe.execute(qry);
    }
}
